package com.yonyou.marketing.app.api.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.marketing.common.Response;
import org.marketing.common.exception.ServiceBizException;
import org.marketing.common.util.CheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.yonyou.marketing.app.api.dto.ViolationDto;
import com.yonyou.marketing.app.api.model.TmRegCities;
import com.yonyou.marketing.app.api.model.TmRegulationsThrid;
import com.yonyou.marketing.app.api.model.TmViolationCity;


@Service("AppViolationInquiryService")
public class AppViolationInquiryService {
	private static final Logger logger = Logger.getLogger(AppViolationInquiryService.class);
	@Autowired
	private ViolationInquiryInterface violationInquiryInterface;
	@Value("${CXZ_KEY}")
	private String CXZ_KEY;
	/**
	 * 查询指定省 发动机号和车架号
	 * @author Administrator
	 * @date 2017年12月4日
	 * @return
	 */
	public Map<String, Object> checkCarReg(String cityCode) {
		logger.info("CXZ_KEY:"+CXZ_KEY);
		Map<String, Object> result = new HashMap<>();
		if("CYX".equals(CXZ_KEY)){
			Response<List<TmRegCities>> rList = violationInquiryInterface.checkCarReg(cityCode);
			if(rList!=null && rList.getResultCode()==1){
				List<TmRegCities> rl = rList.getData();
				if(!CheckUtil.NullOrEmpty(rl)){
					result.put("cityId", rl.get(0).getCityId());
					result.put("cityName", rl.get(0).getName());
					result.put("carCodeLen", rl.get(0).getCarCodeLen());
					result.put("carEngineLen", rl.get(0).getCarEngineLen());
				}
			}else if(rList!=null && rList.getResultCode()==0){
				throw new ServiceBizException(rList.getErrMsg());
			}else{
				throw new ServiceBizException("违章查询服务异常");
			}
		}else if("JH".equals(CXZ_KEY)){
			Response<List<TmViolationCity>> cList = violationInquiryInterface.engineAndVin(cityCode);
			if(cList!=null && cList.getResultCode()==1){
				List<TmViolationCity> cl = cList.getData();
				if(!CheckUtil.NullOrEmpty(cl)){
					if(cl.get(0).getEngine()==0){
						result.put("carEngineLen", 0);
					}else if(cl.get(0).getEngine()==1 && cl.get(0).getEngineno()==0){
						result.put("carEngineLen", 99);
					}else{
						result.put("carEngineLen", cl.get(0).getEngineno());
					}
					if(cl.get(0).getClassa()==0){
						result.put("carCodeLen", 0);
					}else if(cl.get(0).getClassa()==1 && cl.get(0).getClassno()==0){
						result.put("carCodeLen", 99);
					}else{
						result.put("carCodeLen", cl.get(0).getClassno());
					}
					result.put("cityId", cl.get(0).getCityCode());
					result.put("cityName", cl.get(0).getCityName());
				}
			}else if(cList!=null && cList.getResultCode()==0){
				throw new ServiceBizException(cList.getErrMsg());
			}else{
				throw new ServiceBizException("违章查询服务异常");
			}
		}
		return result;
	}
	
	/**
	 * 更新违章车辆信息
	 * @author Administrator
	 * @date 2017年12月4日
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object> updateCarReg(String registNo,String vin,String engine,String userId,String city) {
		logger.info("AppViolationInquiryService.updateCarReg.....");
		Map<String, Object> result = new HashMap<>();
		List<ViolationDto> reList = new ArrayList<>();
		if("CYX".equals(CXZ_KEY)){
			Response<Map<String, Object>> re = violationInquiryInterface.updateCarReg(registNo, vin, engine, userId);
			if(re!=null && re.getResultCode()==1){
				Map<String, Object> map = re.getData();
				List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("list");
				if(!CheckUtil.NullOrEmpty(list)){
					for (Map<String, Object> r : list) {
						ViolationDto v = new ViolationDto();
						v.setAct(r.get("reason").toString());
						v.setActDate(r.get("time").toString());
						v.setActDateExcute(r.get("excuteTime").toString());
						v.setArea(r.get("location").toString());
						v.setDepartment(r.get("department").toString());
						v.setFen(r.get("degree").toString());
						v.setMoney(r.get("count").toString());
						v.setStatus(r.get("status").toString());
						reList.add(v);
					}
				}
				map.put("list", reList);
				result = map;
			}else if(re!=null && re.getResultCode()==0){
				throw new ServiceBizException(re.getErrMsg());
			}else{
				throw new ServiceBizException("违章查询服务异常");
			}
		}else if("JH".equals(CXZ_KEY)){
			Response<Map<String, Object>> re = violationInquiryInterface.violation(registNo, vin, engine,city, userId);
			if(re!=null && re.getResultCode()==1){
				Map<String, Object> map = re.getData();
				List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("list");
				if(!CheckUtil.NullOrEmpty(list)){
					for (Map<String, Object> r : list) {
						ViolationDto v = new ViolationDto();
						v.setAct(r.get("act").toString());
						v.setActDate(r.get("actDate").toString());
						v.setActDateExcute(r.get("excuteTime").toString());
						v.setArea(r.get("area").toString());
						v.setDepartment("");
						v.setFen(r.get("fen").toString());
						v.setMoney(r.get("money").toString());
						v.setStatus(r.get("handled").toString());
						reList.add(v);
					}
				}
				map.put("list", reList);
				result = map;
			}else if(re!=null && re.getResultCode()==0){
				throw new ServiceBizException(re.getErrMsg());
			}else{
				throw new ServiceBizException("违章查询服务异常");
			}
		}
		return result;
	}
	
	/**
	 * 进入违章页面前调用：校验车主
	 * @author Administrator
	 * @date 2017年12月4日
	 * @return
	 */
	public Map<String, Object> checkCar(String userId) {
		Map<String, Object> result = new HashMap<>();
		if("CYX".equals(CXZ_KEY)){
			Response<Map<String, Object>> re = violationInquiryInterface.checkCar(userId);
			if(re!=null && re.getResultCode()==1){
				result = re.getData();
			}else if(re!=null && re.getResultCode()==0){
				throw new ServiceBizException(re.getErrMsg());
			}else{
				throw new ServiceBizException("违章查询服务异常");
			}
		}else if("JH".equals(CXZ_KEY)){
			Response<Map<String, Object>> re = violationInquiryInterface.checkCarJH(userId);
			if(re!=null && re.getResultCode()==1){
				result = re.getData();
			}else if(re!=null && re.getResultCode()==0){
				throw new ServiceBizException(re.getErrMsg());
			}else{
				throw new ServiceBizException("违章查询服务异常");
			}
		}
		return result;
	}
	
	/**
	 * 违章查询历史
	 * @author Administrator
	 * @date 2017年12月4日
	 * @return
	 */
	public List<TmRegulationsThrid> findHistory(String userId) {
		logger.info("AppViolationInquiryService.findHistory.....");
		List<TmRegulationsThrid> result = new ArrayList<>();
		Response<List<TmRegulationsThrid>> re = violationInquiryInterface.findHistory(userId);
		if(re!=null && re.getResultCode()==1){
			result = re.getData();
		}else if(re!=null && re.getResultCode()==0){
			throw new ServiceBizException(re.getErrMsg());
		}else{
			throw new ServiceBizException("违章查询服务异常");
		}
		return result;
	}
	
	/**
	 * 清空历史
	 * @author Administrator
	 * @date 2017年12月4日
	 * @return
	 */
	public Integer deleteHistory(String userId) {
		logger.info("AppViolationInquiryService.deleteHistory.....");
		Integer result = 1;
		Response<Integer> re = violationInquiryInterface.deleteHistory(userId);
		if(re!=null && re.getResultCode()==1){
			result = re.getData();
		}else if(re!=null && re.getResultCode()==0){
			throw new ServiceBizException(re.getErrMsg());
		}else{
			throw new ServiceBizException("违章查询服务异常");
		}
		return result;
	}
	/**
	 * 违章查询简称
	 * @author Administrator
	 * @date 2017年12月4日
	 * @return
	 */
	public List<Map<String,Object>> findProvince() {
		logger.info("AppViolationInquiryService.findProvince.....");
		List<Map<String,Object>> result = new ArrayList<> ();
		if("CYX".equals(CXZ_KEY)){
			Response<List<Map<String,Object>>> re = violationInquiryInterface.findProvince();
			if(re!=null && re.getResultCode()==1){
				result = re.getData();
			}else if(re!=null && re.getResultCode()==0){
				throw new ServiceBizException(re.getErrMsg());
			}else{
				throw new ServiceBizException("违章查询服务异常");
			}
		}else if("JH".equals(CXZ_KEY)){
			Response<List<Map<String,Object>>> re = violationInquiryInterface.citys();
			if(re!=null && re.getResultCode()==1){
				result = re.getData();
			}else if(re!=null && re.getResultCode()==0){
				throw new ServiceBizException(re.getErrMsg());
			}else{
				throw new ServiceBizException("违章查询服务异常");
			}
		}
		return result;
	}
}
